package com.ybg.engine;

import org.springframework.jdbc.core.JdbcTemplate;
import java.util.HashMap;
import java.util.Map;

public class YBGUserType {
	public final static String STUDENT = "1";
	public final static String TEACHER = "2";
	static Map<String,String> identiferMap = new HashMap<String, String>();
	
	public static boolean isTeacher(JdbcTemplate jdbcTemplate,String userId){
		boolean teacherFlag = false;
		if(isTeacherFromCacheOrDB(jdbcTemplate,userId)){
			teacherFlag = true ;
		}
		return teacherFlag;
	}

	private static boolean isTeacherFromCacheOrDB(JdbcTemplate jdbcTemplate,String userId) {
		boolean isTeacher ;
		String flag = identiferMap.get(userId);
		if(flag==null){
			String sql = "select count(*) from YBG_C_TEACHER where gh=?";
			int count = jdbcTemplate.queryForObject(sql, Integer.class, userId);
			if(count>0){
				identiferMap.put(userId, YBGUserType.TEACHER);
				isTeacher =  true ;
			}else{
				identiferMap.put(userId, YBGUserType.STUDENT);
				isTeacher =  false ;
			}
		}else{
			if(flag.equals(YBGUserType.TEACHER)){
				isTeacher = true ;
			}else{
				isTeacher = false;
			}
		}
		return isTeacher;
	}
}